import { getPlayMusic } from "@/http/Home/home";
import { getSongdetail } from '@/http/Play/Play';
import * as types from '../types'
export default {
    // 异步方法
    async [types.PLAY_AND_SET]({ commit,getters,dispatch }, preload) {
        let res = await getPlayMusic({ id:preload.id });
        let response = await getSongdetail({ id: preload.id });
        let lyricResponse = await getSongdetail({ id: preload.id,type:'lyric' });
        console.log(lyricResponse);
        let sourceUrl = res[0].url;
        if (sourceUrl === "") {
            //无资源 改变序号递归自身调用
           let index = preload.index+preload.status;
           let songList = getters.getPlayList;
           let params = songList[index];
           params.index = index;
            dispatch('PLAY_AND_SET',params)
            return false;
        }
        let audio = getters.getAudio;
        audio.setAttribute("src", sourceUrl);
        audio.play();
        commit('SET_SONG_MSG',Object.assign(response.data.songs[0].al, preload.item||{}, { recordIndex: preload.index },{lyric:lyricResponse.data.lrc.lyric||''}))
    },
    async [types.PLAY_AND_SET_LYRIC] ({dispatch}) {
        dispatch('PLAY_AND_SET')
    }
};
